package cn.iocoder.yudao.module.dbit.controller.admin.schmaj;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dbit.controller.admin.schmaj.vo.*;
import cn.iocoder.yudao.module.dbit.dal.dataobject.schmaj.SchMajDO;
import cn.iocoder.yudao.module.dbit.service.schmaj.SchMajService;

@Tag(name = "管理后台 - 共建院校对应专业")
@RestController
@RequestMapping("/dbit/sch-maj")
@Validated
public class SchMajController {

    @Resource
    private SchMajService schMajService;

    @PostMapping("/create")
    @Operation(summary = "创建共建院校对应专业")
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:create')")
    public CommonResult<Integer> createSchMaj(@Valid @RequestBody SchMajSaveReqVO createReqVO) {
        return success(schMajService.createSchMaj(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新共建院校对应专业")
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:update')")
    public CommonResult<Boolean> updateSchMaj(@Valid @RequestBody SchMajSaveReqVO updateReqVO) {
        schMajService.updateSchMaj(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除共建院校对应专业")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:delete')")
    public CommonResult<Boolean> deleteSchMaj(@RequestParam("id") Integer id) {
        schMajService.deleteSchMaj(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得共建院校对应专业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:query')")
    public CommonResult<SchMajRespVO> getSchMaj(@RequestParam("id") Integer id) {
        SchMajDO schMaj = schMajService.getSchMaj(id);
        return success(BeanUtils.toBean(schMaj, SchMajRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得共建院校对应专业分页")
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:query')")
    public CommonResult<PageResult<SchMajRespVO>> getSchMajPage(@Valid SchMajPageReqVO pageReqVO) {
        PageResult<SchMajDO> pageResult = schMajService.getSchMajPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchMajRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出共建院校对应专业 Excel")
    @PreAuthorize("@ss.hasPermission('dbit:sch-maj:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSchMajExcel(@Valid SchMajPageReqVO pageReqVO, HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchMajDO> list = schMajService.getSchMajPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "共建院校对应专业.xls", "数据", SchMajRespVO.class, BeanUtils.toBean(list, SchMajRespVO.class));
    }

}